<?php

class Form extends CI_Controller {

    public function index() {
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        $this->form_validation->set_rules('username', 'Username', 'callback_username_check');
        $this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]|md5');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('myform');
        } else {
            $this->load->view('formsuccess');
        }
    }

    public function username_check($str) {
        if (is_null($str)) {
            $this->form_validation->set_message('username_check', 'The %s field cannot be empty');
            return FALSE;
        } elseif ($str != ucfirst($str)) {
            $this->form_validation->set_message('username_check', 'The %s field has to start with a capital');
            return FALSE;
        } else {
            return TRUE;
        }
    }

}

?>
